package com.csjy.migration.hnnd.business;

import com.csjy.migration.hnnd.DbUtil;
import com.csjy.migration.hnnd.ProgramArgument;
import com.csjy.migration.hnnd.annotation.BusinessDataConverter;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

@BusinessDataConverter(
    auto = false,

    insertSql = "INSERT INTO ACT_HI_DETAIL\n" +
        "(ID_, TYPE_, PROC_INST_ID_, EXECUTION_ID_, TASK_ID_, ACT_INST_ID_, NAME_, VAR_TYPE_, REV_, TIME_, BYTEARRAY_ID_, DOUBLE_, LONG_, TEXT_, TEXT2_)\n" +
        "VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)",

    updateSql = "UPDATE ACT_HI_DETAIL\n" +
    "SET TYPE_=?, PROC_INST_ID_=?, EXECUTION_ID_=?, TASK_ID_=?, ACT_INST_ID_=?, NAME_=?, VAR_TYPE_=?, REV_=?, TIME_=?, BYTEARRAY_ID_=?, DOUBLE_=?, LONG_=?, TEXT_=?, TEXT2_=?\n" +
    "WHERE ID_ = ? ",

    findMigratedIdListSql = "SELECT ID_ FROM ACT_HI_DETAIL WHERE ID_ LIKE ?"
)
public class ActHiDetail extends AbstractBusiness {
  public ActHiDetail(DbUtil sourceDbUtil, DbUtil targetDbUtil, SourceBusinessData sourceData, ProgramArgument programArgument, String projectId) throws SQLException {
    super(sourceDbUtil, targetDbUtil, sourceData, programArgument, projectId);
  }

  @Override
  public void findMigratedIdList() throws SQLException {
    this.migratedIdList = this.targetDbUtil.selectSingleColumn(findMigratedIdListSql, Arrays.asList(new String[]{"%"+this.projectId+"%"}));
  }

  public void migrate() {


  }

  public void customMigrate(Map row) throws SQLException {
    int needMigratedResult = this.needMigrated(this.getCustomSourceId(row.get("ID_").toString(),"PRO_ID"));
    if(needMigratedResult == 0) {
      return;
    }
    if(needMigratedResult == 1) {
      this.targetDbUtil.updateByParams(updateSql,this.buildValues(row),true);
//      this.batchUpdateValues.add(this.buildValues(row));
    } else {
      this.targetDbUtil.updateByParams(insertSql,this.buildValues(row),false);
//      this.batchInsertValues.add(this.buildValues(row));
    }
  }

  public List buildValues(Map row) throws SQLException {
    String procInstId = row.get("ID_") + "-JW";
    List values = new ArrayList();
    values.add(this.getCustomSourceId(row.get("ID_").toString(),"PRO_ID"));
    values.add("VariableUpdate");
    values.add(procInstId);
    values.add(procInstId);
    values.add(null);
    values.add(null);
    values.add("PRO_ID");
    values.add("string");
    values.add(0);
    values.add(new java.sql.Date(System.currentTimeMillis()));
    values.add(null);
    values.add(null);
    values.add(null);
    values.add(row.get("ITEMNO"));
    values.add(null);
    return values;
  }
}
